package com.lzugis.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lzugis.entity.StationInfo;
import com.lzugis.mapper.StationInfoMapper;
import com.lzugis.service.IStationInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @ClassName StationInfoServiceImpl
 * @Author lzugis
 * @Date 2020/9/16 11:32
 **/
@Service
@Slf4j
public class StationInfoServiceImpl extends ServiceImpl<StationInfoMapper, StationInfo> implements IStationInfoService {

    @Override
    public boolean insertByExcel(MultipartFile file) throws Exception {
        if (file == null) {
            return false;
        }
        InputStream in = null;
        try {
            in = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(in);
            XSSFSheet sheet = workbook.getSheetAt(0);
            StationInfo stationInfo = new StationInfo();
            stationInfo.setAdminLevel(sheet.getRow(8).getCell(3).getStringCellValue());
            stationInfo.setBuildArea(Float.valueOf(sheet.getRow(5).getCell(6).getStringCellValue()));
            stationInfo.setCode(sheet.getRow(3).getCell(3).getStringCellValue());
            stationInfo.setName(sheet.getRow(3).getCell(6).getStringCellValue());
            stationInfo.setProvince(sheet.getRow(4).getCell(3).getStringCellValue());
            stationInfo.setLandArea(Float.valueOf(sheet.getRow(4).getCell(6).getStringCellValue()));
            stationInfo.setLon(Float.valueOf(sheet.getRow(5).getCell(3).getStringCellValue()));
            stationInfo.setLat(Float.valueOf(sheet.getRow(6).getCell(3).getStringCellValue()));
            stationInfo.setHeight(Float.valueOf(sheet.getRow(7).getCell(3).getStringCellValue()));
            stationInfo.setGtType(sheet.getRow(6).getCell(6).getStringCellValue());
            stationInfo.setEqType(sheet.getRow(7).getCell(6).getStringCellValue());
            stationInfo.setParentAdmin(sheet.getRow(8).getCell(6).getStringCellValue());
            stationInfo.setProperty(sheet.getRow(11).getCell(3).getStringCellValue());
            stationInfo.setType(sheet.getRow(10).getCell(6).getStringCellValue());
            stationInfo.setCompilePerson(Integer.valueOf(sheet.getRow(11).getCell(2).getStringCellValue()));
            stationInfo.setOnPerson(Integer.valueOf(sheet.getRow(11).getCell(4).getStringCellValue()));
            stationInfo.setOffPerson(Integer.valueOf(sheet.getRow(11).getCell(6).getStringCellValue()));
            this.insert(stationInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        } finally {
            if (in != null) {
                in.close();
            }
        }
        return true;
    }

}
